package com.itcast.order.controller;

import com.itcast.common.UserOrderDTO;
import com.itcast.order.pojo.Order;
import com.itcast.order.service.OrderService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private Mapper dozerMapper;
    @GetMapping("/{username}")
    public List<UserOrderDTO> getOrders(@PathVariable("username") String username){
        List<Order> orders = orderService.findListByUsername(username);

        List<UserOrderDTO> list = new ArrayList<>();
        for (Order order : orders) {
            UserOrderDTO userOrderDTO = dozerMapper.map(order, UserOrderDTO.class);
            list.add(userOrderDTO);
        }
        return list;
    }
}
